package labb2.del1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * Rect.
 *
 * @author Mikko Puustinen
 * @version 0.7, 2007-sep-30
 */
public class Rect extends FillableShape {
    private double width;
    private double height;

    /**
     * Creates a <code>Rect</code>.
     *
     * @param x      start position in x-axis (pixel).
     * @param y      start position in y-axis (pixel).
     * @param color  Color of the <code>Shape</code>.
     * @param width  width.
     * @param height height.
     */
    public Rect(double x, double y, Color color, double width, double height) {
        super(x, y, color);
        this.width = width;
        this.height = height;
    }

    /**
     * Returns the width.
     *
     * @return width.
     */
    public double getWidth() {
        return width;
    }

    /**
     * Returns the height.
     *
     * @return height.
     */
    public double getHeight() {
        return height;
    }

    /**
     * Moves the <code>Rect</code> one step.
     */
    public void move() {
        super.move();
        super.constrain();
        constrain();
    }

    /**
     * Keeps the <code>Rect</code> inside the <code>box</code>.
     */
    public void constrain() {
        Rectangle box = getBox();
        double x0 = box.x;
        double y0 = box.y;
        double x1 = x0 + box.width;
        double y1 = y0 + box.height;
        double dx = getDX();
        double dy = getDY();

        if (getX() + width <= x0) dx = Math.abs(dx);
        if (getX() + width >= x1) dx = -Math.abs(dx);
        if (getY() + height <= y0) dy = Math.abs(dy);
        if (getY() + height >= y1) dy = -Math.abs(dy);

        setVelocity(dx, dy);
    }

    /**
     * Paints the <code>Shape</code>.
     *
     * @param g Graphics object.
     */
    public void paint(Graphics g) {
        g.setColor(getColor());
        if (isFilled())
            g.fillRect((int) getX(), (int) getY(), (int) width, (int) height);
        else
            g.drawRect((int) getX(), (int) getY(), (int) width, (int) height);
    }
}
